package com.py.credit.clean.code;

import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class YearCheck {

    /**
     * 校验输入值为年份格式(必填,时间范围为当前时间的前20年)
     *
     * @param data
     * @param extend
     * @return
     */
    public boolean run(String data, String extend) {
        if (StringUtils.isNotBlank(data)) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
            Calendar calendar = Calendar.getInstance();
            Date current = new Date();
            calendar.setTime(current);
            calendar.add(Calendar.YEAR, -20);
            Date previous = calendar.getTime();

            try {
                Date date = simpleDateFormat.parse(data);

                if (date.after(current) || date.before(previous)) {
                    return false;
                } else {
                    return true;
                }
            } catch (Exception e) {
                return false;
            }
        }

        return false;
    }
}
